package sz.szsmk.citizencard.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;
import androidx.core.widget.NestedScrollView;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.chenenyu.router.annotation.Route;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.RoutineCardActivityBinding;
import sz.szsmk.citizencard.dialog.TwoButtonDialog;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardResponse;
import sz.szsmk.citizencard.mvp.contract.IRoutineCardContact;
import sz.szsmk.citizencard.mvp.presenter.RoutineCardPresenter;
import sz.szsmk.citizencard.ui.adapter.CardAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.widget.CardFunctionView;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/16 15:03
 * description:卡片详情首页
 */
@Route("routinecarddetail")
public class RoutineCardDetailActivity extends IBaseActivity<RoutineCardPresenter, RoutineCardActivityBinding>
        implements IRoutineCardContact.IRoutineCardView, View.OnClickListener {

    public static final String FIRST_CARD_KEY = "first_card_index";
    public static final int MODIFY_CARD_NICK_NAME_REQUEST_CODE = 0x100;

    //    目前第几张卡片
    //    //    目前第几张卡片
    private int currentPosition = 0;

    private CardAdapter cardAdapter;
    private ServiceAdapter serviceAdapter;

    private ArrayList<ArrayList<QueryUserCardResponse.DataBean>> mendList = new ArrayList<>();
    private ArrayList<ArrayList<QueryUserCardResponse.DataBean>> templist = new ArrayList<>();
    private ArrayList<QueryUserCardResponse.DataBean> cardServiceList;

    //    记录省卡与专有账户
    private QueryUserCardResponse.DataBean provincialCard, proprietaryCard;
    private String access_token;

    private int theFirst = 0;

    private int clickCardPosition = -1;
    private QueryUserCardResponse.DataBean clickCardDataBean;
    private String type;
    private String cardNo;
    private String origin;

    @Override
    protected int attachLayoutRes() {
        return R.layout.routine_card_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initSystemBar() {
        ImmersionBar.with(this).init();
    }

    @Override
    protected void initViews() {

        origin = getIntent().getStringExtra("type");
        type = getIntent().getStringExtra(Constant.ORIGIN);
        access_token = ACache.get(this).getAsString("access_token");
        try {
            theFirst = (int) ACache.get(this).getAsObject(FIRST_CARD_KEY);
        } catch (Exception e) {
            theFirst = 0;
        }


//        监听滚动的进度
        dataBinding.scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {

            float alpha = 0;

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                alpha = scrollY / 500f;
                if (alpha < 1.0f) {
                    dataBinding.headLayout.setAlpha(alpha);
                    dataBinding.bjTv.setVisibility(View.VISIBLE);
                    dataBinding.settingTitle.setTextColor(0xffffffff);
                    dataBinding.back.setImageResource(R.drawable.icon_back_white);
                    ImmersionBar.with(RoutineCardDetailActivity.this).statusBarDarkFont(false, 0.0f).init();
                } else {
                    dataBinding.headLayout.setAlpha(1.0f);
                    dataBinding.bjTv.setVisibility(View.GONE);
                    dataBinding.settingTitle.setTextColor(0xff666666);
                    dataBinding.back.setImageResource(R.drawable.screamsearchback);
                    ImmersionBar.with(RoutineCardDetailActivity.this)
                            .statusBarDarkFont(true, 0.0f).init();
                }
                cardAdapter.invalidateBlurView(currentPosition);
            }
        });

//        推荐至首页
        dataBinding.recommendBtn.setOnClickListener(this);
//        交易记录
        dataBinding.bjTv.setOnClickListener(this);
//        云闪充
        dataBinding.cardRecharge.setOnClickListener(this);
//        可用城市查询
        dataBinding.canUseCityTv.setOnClickListener(this);
//        专有账户 去认证按钮
        dataBinding.cardSpeciTv.setOnClickListener(this);
//        删除按钮
        dataBinding.cardDelcard.setOnClickListener(this);
        //修改密码
        dataBinding.tvChangePwdQfk.setOnClickListener(this);
        dataBinding.tvChangePwdQfk2.setOnClickListener(this);


        dataBinding.footprintRl.setOnClickListener(this);

        dataBinding.dzcksjTv.setOnClickListener(this);

        dataBinding.dgkkysjTv.setOnClickListener(this);

        initViewPager();
        initCardInfoList();
    }

    @Override
    public void onClick(View v) {
        /**
         *
         * 点击事件处理
         */
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.recommend_btn) {
            theFirst = currentPosition;
            ACache.get(RoutineCardDetailActivity.this).put(FIRST_CARD_KEY, theFirst);
            updateServiceListView();
        } else if (id == R.id.bj_tv) {
            Bundle bundle1 = new Bundle();
            String cardNo = mendList.get(currentPosition).get(0).getBindCardNo();
            bundle1.putString("cardNo", cardNo);
            JumpUtils.jumptoNatvie(this, "trans", 0, bundle1);
        } else if (id == R.id.card_recharge) {
            Bundle bundle = new Bundle();
            bundle.putString("url", Constant.BASE_URL + "app-front-end/#/charge/main?cardNo=" + mendList.get(currentPosition).get(0).getBindCardNo() + "&access_token=" + access_token);
            String url = Constant.BASE_URL + "app-front-end/#/charge/main?cardNo=" + mendList.get(currentPosition).get(0).getBindCardNo() + "&access_token=" + access_token;
            JumpUtils.jumptoNatvie(RoutineCardDetailActivity.this, "webView", 0, bundle);
        } else if (id == R.id.can_use_city_tv) {//                Toast.makeText(this, "查看更多", Toast.LENGTH_SHORT).show();
            Bundle bundle5 = new Bundle();
            bundle5.putString("url", Constant.BASE_URL + "app-front-end/#/provincialCard/citys?access_token=" + access_token + "&isMain=1");
            JumpUtils.jumptoNatvie(RoutineCardDetailActivity.this, "webView", 0, bundle5);
        } else if (id == R.id.card_speci_tv) {
            String cardNo2 = mendList.get(currentPosition).get(0).getBindCardNo();
            Bundle bundle2 = new Bundle();
            bundle2.putString("cardNo", cardNo2);
            JumpUtils.jumptoNatvie(this, "SpecialAuthActivity", 0, bundle2);
            //修改密码 文化账户  企服卡  是一样的密码
        } else if (id == R.id.tv_changePwd_qfk2 || id == R.id.tv_changePwd_qfk) {
            String cardNo3 = mendList.get(currentPosition).get(0).getBindCardNo();
            Bundle bundle3 = new Bundle();
            bundle3.putString("cardNo", cardNo3);
            JumpUtils.jumptoNatvie(this, "specialpsd", 0, bundle3);
        } else if (id == R.id.footprint_rl) {
            String cardNo;
            Bundle bundle4 = new Bundle();
            cardNo = mendList.get(currentPosition).get(0).getBindCardNo();
            bundle4.putString("url", Constant.BASE_URL + "app-front-end/#/card/footPrint?access_token=" + access_token + "&cardNo=" + cardNo + "&isMain=1");
            JumpUtils.jumptoNatvie(RoutineCardDetailActivity.this, "webView", 0, bundle4);
        } else if (id == R.id.card_delcard) {
            final TwoButtonDialog dialog = new TwoButtonDialog(RoutineCardDetailActivity.this,
                    "确认删除卡片么？");
//            final TwoButtonDialog dialog = new TwoButtonDialog(this, "确认删除历史记录吗");
            dialog.setOnOkClickListener(new TwoButtonDialog.OnOkClickListener() {
                @Override
                public void onOkClick() {
                    dialog.cancel();
//                PubUtils.popTipOrWarn(this, "删除的卡片" + currentPosition);
                }
            });
            dialog.setOnCancelClickListener(new TwoButtonDialog.OnCancelClickListener() {
                @Override
                public void onCancelClick() {
                    String cardNo1 = mendList.get(currentPosition).get(0).getBindCardNo();
                    showLoadingDialog();
                    mPresenter.unBindCard(access_token, cardNo1);
                }
            });
            dialog.show();
        } else if (id == R.id.dzcksj_tv) {//电子钱包  可用商家跳转
            JumpUtils.jump(this, "business", 0);
        } else if (id == R.id.dgkkysj_tv) {
            Bundle bundle6 = new Bundle();
            //id 为 7 跳转下个界面  全部商家的id  7 == 蛋糕卡
            bundle6.putInt("type_id", 7);
            //蛋糕卡 消费商家
            JumpUtils.jumptoNatvie(this, "business", 0, bundle6);
        }
    }

    /**
     * 初始化单个卡片的服务列表
     */
    private void initCardInfoList() {
        serviceAdapter = new ServiceAdapter();
        dataBinding.serviceListView.setAdapter(serviceAdapter);
    }

    /**
     * 初始化卡片横向翻页
     */
    private void initViewPager() {
        cardAdapter = new CardAdapter(RoutineCardDetailActivity.this, mendList, dataBinding.bgView, true);
        dataBinding.cardInfoVp.setAdapter(cardAdapter);
        dataBinding.cardInfoVp.setPageMargin(40);
//        dataBinding.cardInfoVp.setOffscreenPageLimit(3);
        dataBinding.cardInfoVp.setOffscreenPageLimit(20);
        dataBinding.cardInfoVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                cardAdapter.invalidateBlurView(position);
            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                cardAdapter.setCurrentPosition(currentPosition);
                cardServiceList = new ArrayList<>();
                ArrayList<QueryUserCardResponse.DataBean> dataBeans = templist.get(currentPosition);
                String json = new Gson().toJson(dataBeans);
                L.e(Constant.MTAG, "刷新条目" + json + currentPosition);
                cardServiceList.addAll(templist.get(currentPosition));
                updateServiceListView();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        dataBinding.pagerIndicator.setViewPager(dataBinding.cardInfoVp);
        cardAdapter.setPagerOnCardNickNameClickListener(new CardAdapter.PagerOnCardNickNameClickListener() {
            @Override
            public void onCardNickNameClick(int position, QueryUserCardResponse.DataBean dataBean) {
                clickCardPosition = position;
                clickCardDataBean = dataBean;
                Log.d("TAG", "修改前的昵称--> " + dataBean.getCardNickName());
                Intent intent = new Intent(RoutineCardDetailActivity.this, ModifyCardNickNameActivity.class);
                intent.putExtra("cardNo", dataBean.getBindCardNo());
                intent.putExtra("cardNickName", dataBean.getCardNickName());
                startActivityForResult(intent, MODIFY_CARD_NICK_NAME_REQUEST_CODE);
            }

        });

    }

    /**
     * 当卡片切换时重新绘制UI
     */
    private void updateServiceListView() {
        if (theFirst == currentPosition) {
            dataBinding.recommendTxt.setText("已推荐至首页第一张");
            dataBinding.recommendBtn.setVisibility(View.GONE);
            dataBinding.recommendTxt.setTextColor(0xff999999);
        } else {
            dataBinding.recommendTxt.setText("推荐至首页第一张");
            dataBinding.recommendBtn.setVisibility(View.VISIBLE);
            dataBinding.recommendTxt.setTextColor(0xff00aaff);
        }
        if (cardServiceList.get(0) != null) {
            if (!TextUtils.isEmpty(cardServiceList.get(0).getCardTypeName()) && cardServiceList.get(0).getCardTypeName().contains("交通一卡通")) {
                dataBinding.localCardAccountLayout.setVisibility(View.GONE);
                dataBinding.canUseCityLayout.setVisibility(View.GONE);
                dataBinding.privateAccountLayout.setVisibility(View.GONE);
                QueryUserCardResponse.DataBean item = cardServiceList.get(0);
                String funcCode = item.getFuncCode();

                int footPrintStatus = item.getFootPrintStatus();
                int visibility = dataBinding.footprintRl.getVisibility();
                if (funcCode.equals("01") || funcCode.equals("02") || funcCode.equals("03") || funcCode.equals("08")) {
                    if (footPrintStatus == 1) {
                        if (visibility == View.GONE) {
                            dataBinding.footprintRl.setVisibility(View.VISIBLE);
                        }
                    } else {
                        dataBinding.footprintRl.setVisibility(View.GONE);
                    }

                }
            } else {
                ArrayList tempDeleteList = new ArrayList();
                for (QueryUserCardResponse.DataBean item : cardServiceList) {

                    if (!TextUtils.isEmpty(item.getFuncCode()) && item.getFuncCode().equals("04")) {
                        proprietaryCard = new QueryUserCardResponse.DataBean();
                        proprietaryCard.setCardNo(item.getCardNo());
                        proprietaryCard.setIsVerified(item.getIsVerified());
                        proprietaryCard.setCardState(item.getCardState());
                        proprietaryCard.setSpecialAccounts(item.getSpecialAccounts());
                        proprietaryCard.setCakeCardIsOpen(item.getCakeCardIsOpen());
                        proprietaryCard.setCardState(item.getCardState());
                        proprietaryCard.setCakeCardBalance(item.getCakeCardBalance());
                        proprietaryCard.setCardBalance(item.getCardBalance() + "" == null ? 0 : item.getCardBalance());
                        proprietaryCard.setCardTotalBalance(item.getCardTotalBalance() + "" == null ? 0 : item.getCardTotalBalance());
                        proprietaryCard.setIsBusinessOpen(item.getIsBusinessOpen());
                        proprietaryCard.setIsCultureOpen(item.getIsCultureOpen());
                        tempDeleteList.add(item);
                    }
                    if (!TextUtils.isEmpty(item.getFuncCode()) && item.getFuncCode().equals("09"))
                        provincialCard = item;
//                暂时屏蔽 吴江旅游卡 与 月票
                    if (!TextUtils.isEmpty(item.getFuncCode()) && (item.getFuncCode().equals("06") || item.getFuncCode().equals("07")))
                        tempDeleteList.add(item);
//                转转卡不开通就屏蔽
                    if (!TextUtils.isEmpty(item.getFuncCode()) && (item.getFuncCode().equals("08") && item.getCardState().equals("0")))
                        tempDeleteList.add(item);
                }
                cardServiceList.removeAll(tempDeleteList);

                for (QueryUserCardResponse.DataBean item : cardServiceList) {
                    String funcCode = item.getFuncCode();
                    int footPrintStatus = item.getFootPrintStatus();
                    int visibility = dataBinding.footprintRl.getVisibility();
                    if (!TextUtils.isEmpty(funcCode) && (funcCode.equals("01") || funcCode.equals("02") || funcCode.equals("03") || funcCode.equals("08"))) {
                        if (footPrintStatus == 1) {
                            if (visibility == View.GONE) {
                                dataBinding.footprintRl.setVisibility(View.VISIBLE);
                            }
                            break;
                        } else {
                            dataBinding.footprintRl.setVisibility(View.GONE);
                        }
                    }
                }

//            列表中不需要展示这两种
                if (provincialCard != null)
                    cardServiceList.remove(provincialCard);

                if (proprietaryCard != null) {
                    //            判断专有账户是否展示
                    dataBinding.privateAccountLayout.setVisibility(proprietaryCard.getCardState()
                            .equals("1") ? View.VISIBLE : View.GONE);
                    if (proprietaryCard.getIsVerified() == 1) {

                        dataBinding.cardSpeciTv.setVisibility(proprietaryCard.getIsBusinessOpen()==1
                                ? View.VISIBLE : View.GONE);
                        dataBinding.qfkzh.setVisibility(proprietaryCard.getIsBusinessOpen()==1
                                ? View.VISIBLE : View.GONE);
                        //去认证按钮  隐藏
                        dataBinding.cardSpeciTv.setVisibility(View.GONE);
                        //修改密码按钮 显示
                        dataBinding.tvChangePwdQfk.setVisibility(View.VISIBLE);

                        if(proprietaryCard.getIsBusinessOpen()==1){
                            //文字提示
                            dataBinding.tvPwdTip.setVisibility(View.VISIBLE);
                        }else{
                            //文字提示
                            dataBinding.tvPwdTip.setVisibility(View.GONE);
                        }

                        //验证后
                        dataBinding.llVerificationed.setVisibility(View.VISIBLE);
                        dataBinding.tvNoVerification.setVisibility(View.GONE);

                        //如果企服卡  没开通    文化账户那一行  修改密码 显示
                        if(proprietaryCard.getIsBusinessOpen()!=1){
                            dataBinding.tvChangePwdQfk2.setVisibility(View.VISIBLE);
                        }else{
                            dataBinding.tvChangePwdQfk2.setVisibility(View.GONE);
                        }
                        dataBinding.cardQfkzhMoney.setVisibility(View.VISIBLE);
                        dataBinding.cardQfkzhMoney.setText(changeF2Y(proprietaryCard.getSpecialAccounts().getBusinessBalance() + ""));

                    } else {
                        dataBinding.cardSpeciTv.setVisibility(View.VISIBLE);
                        dataBinding.cardSpeciTv.setText("去认证");
                        dataBinding.tvChangePwdQfk.setVisibility(View.GONE);

                        //验证后
                        dataBinding.llVerificationed.setVisibility(View.GONE);
                        dataBinding.tvNoVerification.setVisibility(View.VISIBLE);

                        dataBinding.tvPwdTip.setVisibility(View.GONE);


                        dataBinding.qfkzh.setVisibility(View.VISIBLE);
                        dataBinding.cardQfkzhMoney.setText("-");
                        dataBinding.cardQfkzhMoney.setVisibility(View.GONE);
                        if(proprietaryCard.getIsCultureOpen()==1){
                            dataBinding.cardWhzhMoney.setText("-");
                        }else{
                            dataBinding.cardWhzhMoney.setText("未开通");
                        }
                    }

                    //文化账户是否开通
                    if(proprietaryCard.getIsCultureOpen()==1){
                        dataBinding.cardWhzhMoney.setText(changeF2Y(proprietaryCard.getSpecialAccounts().getCultureBalance() + ""));
                        dataBinding.whzh.setVisibility(View.VISIBLE);
                    }else{
//                            dataBinding.whzh.setText("未开通");
                        dataBinding.whzh.setVisibility(View.GONE);
                    }


                    dataBinding.dgkLin.setVisibility(proprietaryCard.getCakeCardIsOpen() == 0
                            ? View.GONE : View.VISIBLE);

                    dataBinding.cardAllmoney.setText(changeF2Y(proprietaryCard.getCardTotalBalance() + ""));
                    dataBinding.cardDgkzhMoney.setText(changeF2Y(proprietaryCard.getCakeCardBalance() + ""));
                    dataBinding.cardDzqbMoney.setText(changeF2Y(proprietaryCard.getCardBalance() + ""));
                    dataBinding.privateAccountDesc.setText("余额: " + changeF2Y(proprietaryCard.getCardTotalBalance() + "") + " 元");
//                            + CardAdapter.doubleToString() + " 元");
                    cardServiceList.remove(proprietaryCard);
                } else
                    dataBinding.privateAccountLayout.setVisibility(View.GONE);
                dataBinding.localCardAccountLayout.setVisibility(View.GONE);
                dataBinding.canUseCityLayout.setVisibility(View.GONE);
            }

        }


        dataBinding.speedRechargeLayout.setVisibility(View.VISIBLE);
        serviceAdapter.notifyDataSetChanged();
    }

    @Override
    protected void updateViews() {
        showLoadingDialog();
    }

    @Override
    protected void onResume() {
        super.onResume();

        if(getIntent()!=null && getIntent().getExtras().getSerializable("cardList")!=null){
            queryUserCardSuccess((QueryUserCardResponse) getIntent().getExtras().getSerializable("cardList"));
        }else{
            mPresenter.queryUserCard(type, origin);
        }
    }

    /**
     * 展示服务列表的Adapter
     */
    class ServiceAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return cardServiceList == null ? 0 : cardServiceList.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            CardFunctionView cardFunctionView;
            if (view == null)
                cardFunctionView = new CardFunctionView(RoutineCardDetailActivity.this);
            else
                cardFunctionView = (CardFunctionView) view;

            QueryUserCardResponse.DataBean itemBean = cardServiceList.get(position);

            cardFunctionView.setItemBean(itemBean);
            return cardFunctionView;
        }
    }

    private void queryUserCardSuccess(){
        QueryUserCardRes queryUserCardRes = (QueryUserCardRes) getIntent().getExtras().getSerializable("cardList");

        if (Constant.isBind) {
            dataBinding.cardDelcard.setVisibility(View.VISIBLE);
        } else {
            dataBinding.cardDelcard.setVisibility(View.GONE);
        }
        mendList.clear();
        for(QueryUserCardRes.DataBean bean : queryUserCardRes.getData()){

        }





    }


    @Override
    public void queryUserCardSuccess(QueryUserCardResponse response) {

        if (Constant.isBind) {
            dataBinding.cardDelcard.setVisibility(View.VISIBLE);
        } else {
            dataBinding.cardDelcard.setVisibility(View.GONE);
        }

        //四张卡片
        dismissLoadingDialog();
        List<QueryUserCardResponse.DataBean> data = response.getData();
        L.e(Constant.TAG, "data长度" + data.size());
        //入口是专有账户，进行筛选

        if (data != null) {
            ArrayList<String> titles = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                titles.add(data.get(i).getBindCardNo());
            }
            List<String> newList = removeDuplicate(titles);

            mendList.clear();

            for (int i = 0; i < newList.size(); i++) {
                String cardNo = newList.get(i);
                ArrayList<QueryUserCardResponse.DataBean> cardInfo = new ArrayList<>();
                //卡片详情集合
                for (int j = 0; j < data.size(); j++) {
                    QueryUserCardResponse.DataBean dataBean = data.get(j);
                    String cardNo1 = dataBean.getBindCardNo();
                    if (cardNo.equals(cardNo1)) {
                        cardInfo.add(dataBean);
                    }
                }

//                mendList.add(cardInfo);

                if (type != null && type.equals("04")) {
                    if (cardInfo.size() > 3) {
                        QueryUserCardResponse.DataBean dataBean = cardInfo.get(3);
                        if (dataBean != null) {
                            String cardState = dataBean.getCardState();
                            if (cardState != null && cardState.equals("1")) {
                                mendList.add(cardInfo);
                            }
                        }
                    }
                } else {
                    mendList.add(cardInfo);
                }
            }
        }


        dataBinding.pagerIndicator.notifyView();
        cardAdapter.notifyDataSetChanged();
        String json = new Gson().toJson(mendList);
        L.e(Constant.TAG, json);
        templist = deepCopy(mendList);
        if (mendList != null && mendList.size() > 0) {

            cardServiceList = mendList.get(currentPosition);

            updateServiceListView();
            String json2 = new Gson().toJson(mendList);
            L.e(Constant.TAG, json2);
            currentPosition = getIntent().getIntExtra("position", 0);

            dataBinding.cardInfoVp.postDelayed(new Runnable() {
                @Override
                public void run() {
//                    if (currentPosition < 6)
                    dataBinding.cardInfoVp.setCurrentItem(currentPosition, true);
                }
            }, 500);
        }
    }

    public static <E> ArrayList<E> deepCopy(ArrayList<E> src) {
        try {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(src);

            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            @SuppressWarnings("unchecked")
            ArrayList<E> dest = (ArrayList<E>) in.readObject();
            return dest;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<E>();
        }
    }

    @Override
    public void queryUserCardFalie(String msg) {

        dismissLoadingDialog();
    }

    @Override
    public void unBindCardFail(String message) {
        L.e(Constant.TAG, message);
        dismissLoadingDialog();
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void unBindCardSucc() {
        dismissLoadingDialog();
        mendList.remove(currentPosition);
        String json = new Gson().toJson(mendList);
        L.e(Constant.TAG, json);
        if (mendList.size() == 0) {
            finish();
            return;
        }

        dataBinding.pagerIndicator.removeViewAt(currentPosition);
        if (mendList.size() > 0 && currentPosition == mendList.size())
            currentPosition = currentPosition - 1;

        String json2 = new Gson().toJson(mendList);
        L.e(Constant.TAG, json2);
        cardAdapter.notifyDataSetChanged();
        dataBinding.pagerIndicator.onPageSelected(currentPosition);

        cardServiceList = new ArrayList<>();
        cardServiceList.addAll(mendList.get(currentPosition));
        String json3 = new Gson().toJson(mendList);
        L.e(Constant.TAG, json3);
        updateServiceListView();

        dataBinding.scrollView.postDelayed(new Runnable() {
            @Override
            public void run() {
                dataBinding.scrollView.smoothScrollTo(0, 0);
            }
        }, 1200);
        L.e(Constant.TAG, "---------unBindCardSucc-------");
    }

    @Override
    public void modifyCardNickNameSucc() {

    }

    @Override
    public void modifyCardNickNameFail() {

    }

    @Override
    public void queryUserCardInfoTempSucc() {

    }

    @Override
    public void queryUserCardInfoTempFail() {

    }

    public static List<String> removeDuplicate(ArrayList<String> titles) {
        Set set = new LinkedHashSet<String>();
        set.addAll(titles);
        titles.clear();
        titles.addAll(set);
        return titles;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == MODIFY_CARD_NICK_NAME_REQUEST_CODE) {
                clickCardDataBean.setCardNickName(data.getStringExtra("cardNickName"));
                cardAdapter.refreshClickCardView(clickCardPosition, clickCardDataBean);
            }
        }
    }

//    public static String changeF2Y(int price) {
//        return BigDecimal.valueOf(Long.valueOf(price)).divide(new BigDecimal(100)).toString();
//    }

    private String changeF2Y(String amount) {
        NumberFormat format = NumberFormat.getInstance();
        try {
            Number number = format.parse(amount);
            double temp = number.doubleValue() / 100.0;
            format.setGroupingUsed(false);
            // 设置返回的小数部分所允许的最大位数
            format.setMaximumFractionDigits(2);
            amount = format.format(temp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "￥"+amount;
    }

}
